package beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;










import beansInterfaces.PackageMgr;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;









import dto.ExcursionDTO;
import dto.PackageDTO;


import dto.ResidenceDTO;
import dto.TransportDTO;
import entities.GroupEnum;
import entities.PackageEntity;


/**
 * Session Bean implementation class PackageMgrBean
 */
@Stateless
public class PackageMgrBean implements PackageMgr {

	@PersistenceContext
    private EntityManager em;
	
	@Resource
	private EJBContext context;
	
	private BasicProductMgrBean bpmb;
	
    /**
     * Default constructor. 
     */
    public PackageMgrBean() {
        // TODO Auto-generated constructor stub
    }
    
    public List<PackageEntity> getAllPackages(){
    	return em.createNamedQuery(PackageEntity.FIND_ALL, PackageEntity.class).getResultList();    	
    }
    
    public List<PackageEntity> getPackagesForUser(){
    	return em.createNamedQuery(PackageEntity.FIND_FOR_USER, PackageEntity.class).getResultList();    	
    }
    
    public List<PackageDTO> getUserPackagesDTO(){
        Iterator<PackageEntity> iterator = getPackagesForUser().iterator();
        List<PackageDTO> DTOList = new ArrayList<PackageDTO>();
        
         while(iterator.hasNext())
                  {
                          DTOList.add(convertToDTO(iterator.next()));
                  }
                  return DTOList;
    }
    
    public List<PackageDTO> getAllPackagesDTO(){
        Iterator<PackageEntity> iterator = getAllPackages().iterator();
        List<PackageDTO> DTOList = new ArrayList<PackageDTO>();
        
         while(iterator.hasNext())
                  {
                          DTOList.add(convertToDTO(iterator.next()));
                  }
                  return DTOList;
    }
   
    public PackageDTO convertToDTO(PackageEntity p){
    	this.bpmb=new BasicProductMgrBean();
        PackageDTO pDTO = new PackageDTO();
        pDTO.setId(p.getId());
        pDTO.setAvailable(p.isAvailable());
        pDTO.setDisponibility(p.getDisponibility());
        pDTO.setPrice(p.getPrice());
        pDTO.setDiscount(p.getDiscount());
        pDTO.setModified(p.isModified());
        pDTO.setExcursion(this.bpmb.convertToDTO(p.getExcursion()));
        pDTO.setResidence(this.bpmb.convertToDTO(p.getResidence()));
        pDTO.setDepartureTransport(this.bpmb.convertToDTO(p.getDepartureTransport()));
        pDTO.setReturnTransport(this.bpmb.convertToDTO(p.getReturnTransport()));
        return pDTO;
    }
   
    
    
	@Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void addPackage(PackageDTO pDTO) 
	{	
		em.persist(new PackageEntity(pDTO));
		
		

	}
	
	@Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void updatePackage(PackageDTO pDTO) 
	{	
		em.merge(new PackageEntity(pDTO));
		
	}
	
	@Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void deletePackage(PackageDTO packageDTO) 
	{	
		PackageEntity p = em.merge(new PackageEntity(packageDTO));
		em.remove(p);
	}

	@Override
	public boolean isInPack(TransportDTO transportDTO) {
		 Iterator<PackageDTO> iterator = this.getAllPackagesDTO().iterator();
		 long id=transportDTO.getId();
		 
		 while(iterator.hasNext())
		 {	
			 PackageDTO el= iterator.next();
			 if(id== el.getDepartureTransport().getId()
					 ||id== el.getReturnTransport().getId())
				 return true;
		 }
		 return false;
	}

	@Override
	public boolean isInPack(ResidenceDTO residenceDTO) {
	
			 Iterator<PackageDTO> iterator = this.getAllPackagesDTO().iterator();
			 long id=residenceDTO.getId();
			 
			 while(iterator.hasNext())
			 {	
				 PackageDTO el= iterator.next();
				 if(id== el.getResidence().getId())
					 return true;
			 }
			 return false;
	}

	@Override
	public boolean isInPack(ExcursionDTO excursionDTO) {
		 Iterator<PackageDTO> iterator = this.getAllPackagesDTO().iterator();
		 long id=excursionDTO.getId();
		 
		 while(iterator.hasNext())
		 {	
			 PackageDTO el= iterator.next();
			 if(id== el.getExcursion().getId())
				 return true;
		 }
		 return false;
	}
	

	

	

    
	

	
    

}
